package com.pshcp.tool;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.framework.exception.BizException;
import com.framework.tool.CommonTool;
import com.framework.util.DateUtil;
import com.framework.util.JwtUtil;
import com.framework.util.SecurityUtil;
import com.framework.util.SystemUtil;
import com.pshcp.ao.common.ForgotPasswordAO;
import com.pshcp.ao.common.LoginAO;
import com.pshcp.ao.common.PatientWechat2PhoneLoginAO;
import com.pshcp.ao.common.RegisterAO;
import com.pshcp.ao.common.SmsCaptchaAO;
import com.pshcp.ao.common.UpdatePwdAO;
import com.pshcp.ao.common.VerifySmsCaptchaAO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.PatientUser;
import com.pshcp.domain.model.SmsCaptcha;
import com.pshcp.vo.common.DoctorLoginVO;
import com.pshcp.vo.common.LoginVO;
import com.pshcp.vo.common.PatientWechatSessionVO;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/2 22:26
 */
public class AppCommonTool {

    public static void checkSendSmsCode(SmsCaptchaAO smsCaptchaAO) throws BizException {
        CommonTool.checkObj(smsCaptchaAO.getTerminal(), "10010001");
        CommonTool.checkObj(smsCaptchaAO.getType(), "10010002");
        CommonTool.checkString(smsCaptchaAO.getPhone(), "10010003");
        CommonTool.checkObj(smsCaptchaAO.getPurpose(), "10010004");
    }

    public static SmsCaptcha buildSendSmsCode(SmsCaptchaAO smsCaptchaAO) {
        SmsCaptcha smsCaptcha = new SmsCaptcha();
        smsCaptcha.setTerminal(smsCaptchaAO.getTerminal());
        smsCaptcha.setPurpose(smsCaptchaAO.getPurpose());
        smsCaptcha.setType(smsCaptchaAO.getType());
        smsCaptcha.setPhone(smsCaptchaAO.getPhone());
        smsCaptcha.setCode(SystemUtil.getRandomCode(6));
        smsCaptcha.setBizStatus(0);
        return smsCaptcha;
    }

    public static void checkVerifySmsCaptcha(VerifySmsCaptchaAO verifySmsCaptchaAO) throws BizException {
        CommonTool.checkObj(verifySmsCaptchaAO.getSmsPkId(), "10010007");
        CommonTool.checkObj(verifySmsCaptchaAO.getSmsCode(), "10010008");
    }

    public static void checkRegister(RegisterAO registerAO) throws BizException {
        CommonTool.checkString(registerAO.getPhone(), "10010010");
        CommonTool.checkString(registerAO.getPassword(), "10010011");
        CommonTool.checkObj(registerAO.getSmsPkId(), "10010012");
    }


    public static PatientUser buildPatientUserRegister(RegisterAO registerAO) throws Exception {
        PatientUser patientUser = new PatientUser();
        patientUser.setId(SystemUtil.getId());
        patientUser.setBizStatus(0);
        patientUser.setPhone(registerAO.getPhone());
        patientUser.setPassword(SecurityUtil.MD5(registerAO.getPassword()));
        patientUser.setCreatedTime(DateUtil.currDate());
        return patientUser;
    }

    public static void checkLogin(LoginAO loginAO) throws BizException {
        CommonTool.checkObj(loginAO.getType(), "10010019");
        CommonTool.checkString(loginAO.getPhone(), "10010020");
        if (loginAO.getType() == 0) {
            CommonTool.checkString(loginAO.getPassword(), "10010021");
        }
        if (loginAO.getType() == 1) {
            CommonTool.checkObj(loginAO.getSmsPkId(), "10010022");
            CommonTool.checkString(loginAO.getSmsCode(), "10010023");
        }
    }

    public static void checkUpdatePassword(UpdatePwdAO updatePwdAO) throws BizException {
        CommonTool.checkString(updatePwdAO.getPassword(), "10010033");
        CommonTool.checkObj(updatePwdAO.getSmsPkId(), "10010034");
        CommonTool.checkString(updatePwdAO.getSmsCode(), "10010035");
    }

    public static Doctor buildDoctorUpdatePassword(Doctor doctor, String pwd) throws Exception {
        doctor.setPassword(SecurityUtil.MD5(pwd));
        return doctor;
    }

    public static void checkForgotPassword(ForgotPasswordAO forgotPasswordAO) throws BizException {
        CommonTool.checkString(forgotPasswordAO.getPhone(), "10010028");
        CommonTool.checkString(forgotPasswordAO.getPassword(), "10010029");
        CommonTool.checkObj(forgotPasswordAO.getSmsPkId(), "10010030");
    }

    public static PatientUser buildPatientUserForgotPassword(PatientUser patientUser, ForgotPasswordAO forgotPasswordAO) throws Exception {
        patientUser.setPassword(SecurityUtil.MD5(forgotPasswordAO.getPassword()));
        return patientUser;
    }

    public static Doctor buildDoctorForgotPassword(Doctor doctor, ForgotPasswordAO forgotPasswordAO) throws Exception {
        doctor.setPassword(SecurityUtil.MD5(forgotPasswordAO.getPassword()));
        return doctor;
    }

    public static PatientWechatSessionVO buildPatientWechatSessionVO(WxMaJscode2SessionResult wxMaJscode2SessionResult, PatientUser patientUser, String token,Long userId) throws Exception {
        PatientWechatSessionVO patientWechatSessionVO = new PatientWechatSessionVO();
        patientWechatSessionVO.setRegisterFlag(patientUser == null ? 0 : 1);
        patientWechatSessionVO.setOpenid(wxMaJscode2SessionResult.getOpenid());
        patientWechatSessionVO.setSessionKey(wxMaJscode2SessionResult.getSessionKey());
        String checkSignature = SecurityUtil.MD5(wxMaJscode2SessionResult.getOpenid() + "_" + (wxMaJscode2SessionResult.getSessionKey()));
        patientWechatSessionVO.setCheckSignature(checkSignature);
        patientWechatSessionVO.setToken(token);
        patientWechatSessionVO.setUserId(userId);
        return patientWechatSessionVO;
    }

    public static PatientUser buildPatientUserWechatRegister(String openid, String phone) throws Exception {
        PatientUser patientUser = new PatientUser();
        patientUser.setId(SystemUtil.getId());
        patientUser.setBizStatus(0);
        patientUser.setOpenid(openid);
        patientUser.setPhone(phone);
        return patientUser;
    }
    public static PatientUser buildPatientUserWechatRegister(String phone) throws Exception {
        PatientUser patientUser = new PatientUser();
        patientUser.setId(SystemUtil.getId());
        patientUser.setBizStatus(0);
        patientUser.setPhone(phone);
        return patientUser;
    }
    public static PatientUser buildPatientUserWechatRegister(PatientUser patientUser,String openid) throws Exception {
        patientUser.setBizStatus(0);
        patientUser.setOpenid(openid);
        return patientUser;
    }

    public static PatientUser buildPatientUserDouYinRegister(String openidDy, String anonymousOpenid) throws Exception {
        PatientUser patientUser = new PatientUser();
        patientUser.setId(SystemUtil.getId());
        patientUser.setBizStatus(0);
        patientUser.setOpenidDy(openidDy);
        patientUser.setAnonymousOpenid(anonymousOpenid);
        return patientUser;
    }

    public static void checkPatientWechat2PhoneLogin(PatientWechat2PhoneLoginAO patientWechat2PhoneLoginAO) throws BizException {
        CommonTool.checkString(patientWechat2PhoneLoginAO.getPhone(), "10010020");
        CommonTool.checkObj(patientWechat2PhoneLoginAO.getSmsPkId(), "10010022");
        CommonTool.checkString(patientWechat2PhoneLoginAO.getSmsCode(), "10010023");
    }


    public static DoctorLoginVO buildLoginVO(String token, Long userId,String name) {
        DoctorLoginVO loginVO = new DoctorLoginVO();
        loginVO.setToken(token);
        loginVO.setUserId(userId);
        loginVO.setDoctorName(name);
        return loginVO;
    }
    public static LoginVO buildLoginVO(String token, Long userId) {
        LoginVO loginVO = new LoginVO();
        loginVO.setToken(token);
        loginVO.setUserId(userId);
        return loginVO;
    }
}
